package com.xnchat.pro.service;

import com.xnchat.pro.dto.Message;
import com.xnchat.pro.mapper.MessageRepository;
import com.xnchat.pro.mapper.RoomRepository;
import com.xnchat.pro.vo.MessageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Autowired
    MessageRepository messageRepository;
    @Autowired
    RoomRepository roomRepository;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 普通消息保存类
     *
     * @param name     啊
     * @param tripcode 啊
     * @param message  啊
     * @param time     啊
     * @param msgtype  啊
     * @param mmm      啊
     * @return 保存成功
     */

    public boolean savemessage(String name, String tripcode
            , String message, String time, String msgtype, MessageVO mmm) {

        Message msg = new Message();
        msg.setName(name);
        msg.setMessage(message);
        msg.setTripcode(tripcode);
        msg.setMessagetype(msgtype);
        logger.info(name + ":" + message);
        String[] split = time.split("\\.");
        time = split[0];
        msg.setMessagetime(new Date(Long.parseLong(time) * 1000L));

        msg.setRoomid(mmm.getRoomId());
        messageRepository.save(msg);
        return true;
    }

    /**
     * 存在url信息的消息保存类
     *
     * @param name
     * @param tripcode
     * @param message
     * @param time
     * @param url
     * @param msgtype
     * @param mmm
     * @return
     */
    public boolean saveByUrl(String name, String tripcode
            , String message, String time, String url, String msgtype, MessageVO mmm) {
        Message msg = new Message();
        msg.setName(name);
        msg.setMessage(message);
        logger.info(name + ":" + message);
        msg.setTripcode(tripcode);
        msg.setUrl(url != null ? url : "empty");
        msg.setMessagetype(msgtype);
        msg.setMessagetype(mmm.getTalks().get(0).getType());
        String[] split = time.split("\\.");
        time = split[0];
        msg.setMessagetime(new Date(Long.parseLong(time) * 1000L));

        msg.setRoomid(mmm.getRoomId());
        messageRepository.save(msg);
        return true;

    }

    /**
     * 分页查询所有消息
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Message> msglist(int page, int size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "messagetime");
        Pageable pageable = PageRequest.of(page, size, sort);
        return messageRepository.findAll(pageable);
    }

    /**
     * 根据name查询
     *
     * @param page
     * @param size
     * @param names
     * @return
     */
    public Page<Message> selectBynames(int page, int size, List<String> names) {
        Sort sort = Sort.by(Sort.Direction.DESC, "messagetime");
        Pageable pageable = PageRequest.of(page, size, sort);
        return messageRepository.findByNameIn(names, pageable);
    }

    public int countByName(List<String> names) {
        return messageRepository.countByNameIn(names);
    }

    /**
     * 总数获取 分页查询所有消息用
     *
     * @return
     */
    public int countAll() {
        return messageRepository.countAll();
    }

    public Page<Message> findByRoomId(int page, int size, String roomId) {
        return messageRepository.findAllByRoomid(roomId,PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "messagetime")));
    }
    public int countByRoomId(String roomid){
        return messageRepository.countAllByRoomid(roomid);
    }
}

